<?php
/**
 *   Copyright (C) 2011 - Casey Burkhardt
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
**/

require_once("functions.inc.php");

if (strcmp($_GET["raw"], "true") === 0) {
  if (isInstanceActive()) {
	$response["active"] = TRUE;
    $response["location"] = getLocation();
    $response["tightness_level"] = getTightnessLevel();
    $response["tightness_description"] = getTightnessDescription($response["tightness_level"]);
  } else {
	$response["active"] = FALSE;
  }
  die(json_encode($response));
}

// Disable Inactive Tightometers
if (!isInstanceActive()) {
  header("Location: http://tightometer.com");
}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <title><?php echo getTitle(); ?></title>
    <script type="text/javascript" src="./res/json.js"></script>
	<script type="text/javascript">
    var xmlhttp = false;
    var imageUrl = "http://chart.apis.google.com/chart?cht=gom&chs=500x300&chco=00FF00,FFFF00,FF0000&chl=";
    function init() {
      try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
        try {
          xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {
          xmlhttp = false;
        }
      }

      if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
        try {
          xmlhttp = new XMLHttpRequest();
        } catch (e) {
          xmlhttp=false;
        }
      }

      if (!xmlhttp && window.createRequest) {
        try {
          xmlhttp = window.createRequest();
        } catch (e) {
          xmlhttp=false;
        }
      }
      window.setInterval('refreshImage();', <?php echo getRefreshRate(); ?>);
    }

    function refreshImage() {
      xmlhttp.open("GET", "status.php?raw=true",true);
      xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState == 4) {
          var response = jsonParse(xmlhttp.responseText);
          var newImage = imageUrl + response.tightness_description + "&chd=t:" + response.tightness_level + "&chls=5|10&chxt=x&chxs=0,000000,12";
          document.getElementById("tightometer").src = unescape(newImage);
        }
      }
      xmlhttp.send(null)
    }

    </script>
  </head>
  <body onload="init();">
  <h1><?php echo getTitle(); ?></h1>
  <?php
    $tightnessLevel = getTightnessLevel();
    $tightnessDescription = getTightnessDescription($tightnessLevel);
  ?>
  <div align="center">
    <img src="http://chart.apis.google.com/chart?cht=gom&chs=500x300&chco=00FF00,FFFF00,FF0000&chl=<?php echo $tightnessDescription; ?>&chd=t:<?php echo $tightnessLevel; ?>&chls=5|10&chxt=x&chxs=0,000000,12" id="tightometer" width="75%">
  </div>
  </body>
</html>
